var isLoaded=false;
$(function() {
	if(!isLoaded){
		setWXConfig(document.location.href);
	// 业务类型选择部分
	$('.wrap .header .next a').bind('click', function(event) {
		$('.main').hide();
		$('.business-main').show();
	});
	$('.business-ch').unbind('click');
	// 不同的业务类型对应不同的页面
	$('.business-ch').bind(
			'click',
			function(event) {
				var decorate = "用户新装";
				var repair = "故障报修";
				var watermeter = "水表检测";
				var destory = "用户销户";
				var typeObj = $(this).find('.type');
				var type = typeObj.text();
				
				$('#name').val('');
				$('#phone').val('');
				$('#identity').val('');
				$('#phoneNumber').val('');
				$('#userAddress').val('');
				//用于清除两张图片的serverId信息
				images.downloadImage="";
				images.previewImage="";
				while(document.getElementById("previewImage").hasChildNodes()){ 
					document.getElementById("previewImage")
					.removeChild(document.getElementById("previewImage").firstChild);
		        }
				
				while(document.getElementById("downloadImage").hasChildNodes()){ 
					document.getElementById("downloadImage")
					.removeChild(document.getElementById("downloadImage").firstChild);
		        }
				
				$("#applyType").val(typeObj.attr("applyType"));
				if (decorate == type) {
					$(this).find('.state').text('已选择').parent().siblings(
							'.business-ch').find('.state').text('');
					$('.confirm').css({
						'margin-top' : '56%'
					});
					$('.wrap .header .title .new-user').text(type);
					$('.business-main').hide();
					$('.main').show();
					$('.decorate').show();
					$('.repair').hide();

				} else {
					$(this).find('.state').text('已选择').parent().siblings(
							'.business-ch').find('.state').text('');
					$('.confirm').css({
						'margin-top' : '88%'
					});
					$('.wrap .header .title .new-user').text(type);
					$('.business-main').hide();
					$('.main').show();
					$('.decorate').hide();
					$('.repair').show();
					$(".serial-main").hide();
					
				}

			});
	// 上传身份证照片 上传身份证图片
	$('.upload-main .upload').bind('click', function(event) {
		$('.sel-img').show();
		$('.confirm').css({
			'margin-top' : '26%'
		});
	});
	
	queryUserHsList();
	isLoaded=true;
	}
});

function submitApplyInfo(obj) {
	
	var applyType = $("#applyType").val();
	var param = getUrlParameters();
	var homeUserNo = $("#userNo").val();
	// 获取信息并进行判断
	var name = $("#name").val();
	var phone = $("#phone").val();
	var userAddress = $("#userAddress").val();
	//防止切换div时无法获取到对应div中的phone信息
	if(phone == null || phone == ""){
		phone = $("#phoneNumber").val();
	}
	var identity = $("#identity").val();
	var userId = param.userId || "";
	
	var serverIds = "";
	
	if(applyType == "01"){

		if (name == null || name == '') {
			alert("姓名不能为空");
			return;
		}
		var byteLength = $.trim(name).replace(/[^\x00-\xff]/ig, "**").length;

		if(byteLength > 50){
			alert("姓名长度过长");
			return;
		}
		var reg = /^[\u4e00-\u9fa5A-Za-z]+$/;
		if(!reg.test(name)){
			alert("姓名格式不正确，只能输入中文和字母");
			return;
		}
		
		if(!checkIdNo(identity)){
			alert("身份证输入错误");
			return;
		} 
		
		if(images.downloadImage == "" || images.downloadImage == null
			|| images.previewImage == "" || images.previewImage == null ){
		alert("请选择上传图片！");
		return;
		}
		
		serverIds = images.downloadImage + "|" + images.previewImage;
		
		if (userAddress == null || $.trim(userAddress) == "") {
			alert("内容不能为空");
			return false;
		}
		
		var byteLength = $.trim(userAddress).replace(/[^\x00-\xff]/ig, "**").length;
		if(byteLength>100){
			alert("输入地址长度过长");
			return ;
		}
	}
	
	if (phone == null || phone == '') {
		alert("手机不能为空");
		return;
	}
	if (phone != null) {
		var reg = /^1[3|4|5|8][0-9]\d{4,8}$/;
		if (!reg.test(phone)) {
			alert("手机号码格式不对！");
			return;
		}
	}
	
	$.ajax({
		url : preUrl + "applyDetail/submitApplyDetailInfo",
		type : "POST",
		dataType : "json",
		data : {
			"userName" : $.trim(name),
			"mobile" : $.trim(phone),
			"identity" : $.trim(identity),
			"applyUserId" : userId,
			"serverIds" : serverIds,
			"homeUserNo" : homeUserNo,
			"applyType" : applyType,
			"userAddress" : $.trim(userAddress)
		},
		success : function(data) {
			if (data.rspCode == "0000") {
				alert(data.rspMsg);
				window.location.href=preUrl+"wechat/user/to_query_progress.html";
				return;
			}else{
				alert(data.rspMsg||"受理失败，请稍后重试！");
				return;
			}
		}
	});
}


//查询用户已绑定房源列表
function queryUserHsList() {
	var param=getUrlParameters();
	var userId=param.userId;
	var userNoHome=param.homeUserNo;
	$("#hsList").html("");
	$.ajax({
		url :preUrl+"common/getUserId",
		type : "POST",
		dataType : "json",
		data : {"userId" : userId},
		success : function(data) {
			var divTmp=$("<div></div>");
			for ( var i = 0; i < data.data.length; i++) {
				var temp = $("#hsInfosTemplate").clone().show().removeAttr("id");
				var pBuild=temp.find("p.building");
				pBuild.append($("<span></span>").text(data.data[i].HS_USER_NO+" "));
				pBuild.attr("val",data.data[i].HS_USER_NO);
				pBuild.attr("hsname",data.data[i].HS_NAME);

				pBuild.append(data.data[i].HS_NAME);
				var selected = "";
				// 是房源的userNO 如果userNo为这个则表示已选中
				if (data.data[i].HS_USER_NO == userNoHome) {
					selected = "已选择";
					$("#userNo").val(userNoHome);
					$("#hsname").text(data.data[i].HS_NAME);
				}
				if (data.data.length - 1 == i) {
					temp.addClass("serial-ch00");
				}
				temp.find("p.state").html(selected);
				divTmp.append(temp);
			}
			
			$("#hsList").html(divTmp.html());
		}
	});

}

//显示选择列表
function userThisHs(obj){
	var evObj=$(obj).find(".building");
	$('.main').show();
	$('.repair').show();
	$(".serial-main").hide();
	$("#userNo").val(evObj.attr("val"));
	$("#hsname").text(evObj.attr("hsname"));
	$("#hsList").find(".state").text("");
	$(obj).find(".state").text("已选择");
	
}
//显示房源
function selectHs(){
	$('.main').hide();
	$('.business-main').hide();
	$('.repair').hide();
	$(".serial-main").show();
}